﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Mmht.BT;

public partial class Homepage : BasePage
{
    NoticeBT nt = new NoticeBT();
    GetPdf gp = new GetPdf();
    GetMovie gm = new GetMovie();
    GetImage gi = new GetImage();
    public bool isAdmin = false;
    public string content;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["type"] == null)
        {
            Response.Redirect("login.aspx");
            return;
        }
        if (!IsPostBack)
        {
            controlrepeater1();
            setUserInfo();
            getInfo();
        }
        this.lbChangeLanguage.Text = toLanguage;
    }

    private void controlrepeater1()
    {
        DataTable dt1 = nt.GetLatestNotice(8);
        DataTable dt2 = gp.Get_CTRtop_Pdf("课件", 7);
        DataTable dt3 = gp.Get_CTRtop_Pdf("文献", 7);
        DataTable dt4 = gp.Get_CTRtop_Pdf("教研文章", 7);
        DataTable dt5 = gm.Get_CTRtop_Movie(5);
        DataTable dt6 = gi.GetAllActiveImages();

        this.Repeater6.DataSource = dt6;
        this.Repeater6.DataBind();
        this.Repeater1.DataSource = dt1;
        this.Repeater1.DataBind();
        this.Repeater2.DataSource = dt2;
        this.Repeater2.DataBind();
        this.Repeater3.DataSource = dt3;
        this.Repeater3.DataBind();
        this.Repeater4.DataSource = dt4;
        this.Repeater4.DataBind();
        this.Repeater5.DataSource = dt5;
        this.Repeater5.DataBind();

    }

    public string CheckEmergency(bool emengency)
    {
        if (emengency)
        {
            return " style='Color:red;' ";
        }
        else
        {
            return "style='Color:#326FA8;'";
        }
    }

    public string getViewHostClass(Object number)
    {
        return ViewHotBT.getViewHostClass(number);
    }

    /// <summary>
    /// 切换语言版本
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void lbChangeLanguage_Click(object sender, EventArgs e)
    {
        LinkButton lb = (LinkButton)sender;
        string lv = string.Empty;
        if (lb.Text.Trim().ToLower().Contains("english"))
        {
            lv = "en-US";
        }
        else
        {
            lv = "cn-Zh";
        }
        Response.Redirect("ChangeLanguage.aspx?Language=" + lv + "&url=" + Server.UrlEncode(Request.Url.PathAndQuery), true);
    }
    /// <summary>
    /// 修改个人信息
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btModify_Click(object sender, EventArgs e)
    {
        string name = this.tbUserName.Text.Trim();
        string pwd = this.tbPwd.Text.Trim();
        string conPwd = this.tbConPwd.Text.Trim();
        string email = this.tbUserEmail.Text.Trim();
        string phone = this.tbUserPhone.Text.Trim();
        if (pwd != conPwd)
        {
            this.imgPwd.Visible = true;
            this.imgConPwd.Visible = true;
            return;
        }
        string type = Session["type"].ToString();
        if (type == "normal")
        {
            StudentBT sbt = new StudentBT();
            int sid = int.Parse(Session["student_id"].ToString());
            bool rst = sbt.UpdateStudent(sid, name, pwd, email, phone);
            if (rst)
            {
                this.lbMsg.Text = "修改成功";
                setUserInfo();
            }
            else
            {
                this.lbMsg.Text = "修改失败";
            }
        }
        else if (type == "admin")
        {
            AdminBT abt = new AdminBT();
            int aid = int.Parse(Session["admin_id"].ToString());
            bool rst = abt.UpdateAdmin2(pwd, email, phone, aid);
            if (rst)
            {
                this.lbMsg.Text = "修改成功";
                setUserInfo();
            }
            else
            {
                this.lbMsg.Text = "修改失败";
            }
        }
    }
    /// <summary>
    /// 设置用户信息
    /// </summary>
    private void setUserInfo()
    {
        string account = "";
        string name = "";
        string email = "";
        string phone = "";
        string type = Session["type"].ToString();
        if (type == "normal")
        {
            StudentBT sbt = new StudentBT();
            int sid = int.Parse(Session["student_id"].ToString());
            DataRow row = sbt.FindById(sid);
            if (row == null)
            {
                Response.Redirect("login.aspx");
            }
            else
            {
                account = row["account"].ToString();
                name = row["name"].ToString();
                email = row["e_mail"].ToString();
                phone = row["tel"].ToString();
            }
        }
        else if (type == "admin")
        {
            isAdmin = true;
            AdminBT abt = new AdminBT();
            int aid = int.Parse(Session["admin_id"].ToString());
            DataTable table = abt.Datarowbyid(aid);
            if (table == null)
            {
                Response.Redirect("login.aspx");
                return;
            }
            if (table.Rows.Count == 0)
            {
                Response.Redirect("login.aspx");
                return;
            }
            else
            {
                DataRow row = table.Rows[0];
                account = row["name"].ToString();
                name = row["name"].ToString();
                email = row["e_mail"].ToString();
                phone = row["tel"].ToString();
            }
        }
        this.lbUserAcount.Text = account;
        this.lbUserName .Text  = name;
        this.lbUserEmail .Text = email;
        this.tbUserName.Text = name;
        this.tbUserEmail.Text = email;
        this.tbUserPhone.Text = phone;
        //this.lbUserPhone .Text = phone;
    }

    /// <summary>
    /// 获取课程简介信息
    /// </summary>
    private void getInfo()
    {
        string type = "课程介绍_课程简介";
        PageBT pbt = new PageBT();
        string html = pbt.GetContent(type);
        string StrNohtml = System.Text.RegularExpressions.Regex.Replace(html, "<[^>]+>", "");
        StrNohtml = System.Text.RegularExpressions.Regex.Replace(StrNohtml, "&[^;]+;", "");
        StrNohtml = StrNohtml.Replace("\r\n", "");
        content = string.IsNullOrEmpty(StrNohtml) ? "暂无简介" : (StrNohtml.Substring(0, 250) + " ……");
    }
    /// <summary>
    /// 登出
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btLogout_Click(object sender, EventArgs e)
    {
        Session.Clear();
        Response.Redirect("login.aspx");
    }
}
